package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.R;
import com.example.demo.common.S;
import com.example.demo.dto.SetmealDto;
import com.example.demo.entity.Category;
import com.example.demo.entity.Setmeal;
import com.example.demo.service.CategoryService;
import com.example.demo.service.SetmealDishService;
import com.example.demo.service.SetmealService;
import com.sun.org.apache.xerces.internal.impl.xpath.XPath;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    SetmealService setmealService;
    @Autowired
    SetmealDishService setmealDishService;
    @Autowired
    CategoryService categoryService;
    //新增套餐
    @PostMapping
    public S<String> save(@RequestBody SetmealDto setmealDto){
        setmealDto.toString();
        setmealService.saveWithDish(setmealDto);
        return S.success("添加成功");
    }
    //分页查询
    @GetMapping("/page")
    public S<Page<SetmealDto>> page(int page, int pageSize, String name){
        //分页构造器
        Page<Setmeal> page1=new Page(page,pageSize);
        Page<SetmealDto> page2=new Page<>();
        //添加查询条件
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(page1,lambdaQueryWrapper);
        BeanUtils.copyProperties(page1,page2,"records");
        List<Setmeal> records = page1.getRecords();
        List<SetmealDto> collect = records.stream().map((steam) -> {
            SetmealDto setmealDto = new SetmealDto();
            //拷贝对象
            BeanUtils.copyProperties(steam, setmealDto);
            //获取分类id
            Long categoryId = steam.getCategoryId();
            //通过id查询对象
            Category byId = categoryService.getById(categoryId);
            if (byId.getName() != null) {
                setmealDto.setCategoryName(byId.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());
        page2.setRecords(collect);
        return S.success(page2);
    }
    //删除套餐
    @DeleteMapping
    public S<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return S.success("套餐数据删除");
    }
    //根据条件查询套餐数据
    @GetMapping("/list")
    public S<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return S.success(list);
    }
}
